package com.jaya.budan.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jaya.budan.R;
import com.jaya.budan.bean.VIP_JiLu;
import com.jaya.budan.views.Circle_ImageView;

import java.util.ArrayList;
import java.util.List;

public class VIP_ZX_HorizontalListViewAdapter extends BaseAdapter {

    private Context mcontext;
    private int screenWidth, mtype, select = -1;
    private List<String> list_name;
    private List<Integer> list_icon;
    private ArrayList<VIP_JiLu> mlist;
    private ArrayList<VIP_JiLu.Detail>  detailList;

    public int getSelect() {
        return select;
    }

    public void setSelect(int select) {
        this.select = select;
    }

    public VIP_ZX_HorizontalListViewAdapter(Context context, int width, List<String> name, List<Integer> icon,
                                            int type, ArrayList<VIP_JiLu> vip_jiLus, ArrayList<VIP_JiLu.Detail> details){
        mcontext = context;
        screenWidth = width;
        list_name = name;
        list_icon = icon;
        mtype = type;
        mlist = vip_jiLus;
        detailList = details;
    }

    @Override
    public int getCount() {
        if (mtype == 1){
            return list_name.size();
        } else if (mtype == 3){
            return mlist.size();
        } else if (mtype == 5){
            return mlist.size();
        } else if (mtype == 4){
            return detailList.size();
        } else {
            return mlist.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (mtype == 1){
            return list_name.get(position);
        } else {
            return mlist.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (mtype == 1){
            convertView = View.inflate(mcontext, R.layout.main_listview_item, null);
            LinearLayout ll_main_item = convertView.findViewById(R.id.ll_main_item);
            TextView tv_main_lv_item = convertView.findViewById(R.id.tv_main_lv_item);
            ImageView iv_fire = convertView.findViewById(R.id.iv_fire);
            Circle_ImageView iv_main_item = convertView.findViewById(R.id.iv_main_item);
            ViewGroup.LayoutParams layoutParams = ll_main_item.getLayoutParams();
            layoutParams.width = screenWidth / 9 * 2;
            ll_main_item.setLayoutParams(layoutParams);
            iv_fire.setVisibility(View.GONE);
            tv_main_lv_item.setText("" + list_name.get(position));
            iv_main_item.setImageResource(list_icon.get(position));
            tv_main_lv_item.setMovementMethod(ScrollingMovementMethod.getInstance());
            tv_main_lv_item.setHorizontallyScrolling(true); //不让超出屏幕的文本自动换行，使用滚动条
            tv_main_lv_item.setFocusable(true);
            return convertView;
        } else if (mtype == 3){
            convertView = View.inflate(mcontext, R.layout.lv_gerentg_buy_item, null);
            ImageView iv_gerentg_buy_icon = convertView.findViewById(R.id.iv_gerentg_buy_icon);
            TextView tv_gerentg_buy_dt_1 = convertView.findViewById(R.id.tv_gerentg_buy_dt_1);
            TextView tv_gerentg_item_money = convertView.findViewById(R.id.tv_gerentg_item_money);
            Glide.with(mcontext).load(mlist.get(position).getIcon()).apply(new RequestOptions()
                    .placeholder(R.mipmap.iv_main_item).error(R.mipmap.iv_main_item)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//关闭Glide的硬盘缓存机制
            ).into(iv_gerentg_buy_icon);
            tv_gerentg_buy_dt_1.setText("" + mlist.get(position).getName());
            tv_gerentg_item_money.setText("" + mlist.get(position).getPrice());
            tv_gerentg_buy_dt_1.setMovementMethod(ScrollingMovementMethod.getInstance());
            tv_gerentg_buy_dt_1.setHorizontallyScrolling(true); //不让超出屏幕的文本自动换行，使用滚动条
            tv_gerentg_buy_dt_1.setFocusable(true);
            return convertView;
        } else if (mtype == 5){
            convertView = View.inflate(mcontext, R.layout.lv_gerentg_item, null);
            ImageView iv_gerentg_item_icon = convertView.findViewById(R.id.iv_gerentg_item_icon);
            TextView tv_gerentg_30 = convertView.findViewById(R.id.tv_gerentg_30);
            TextView tv_gerentg_30_num = convertView.findViewById(R.id.tv_gerentg_30_num);
            Glide.with(mcontext).load(mlist.get(position).getIcon()).apply(new RequestOptions()
                    .placeholder(R.mipmap.iv_main_item).error(R.mipmap.iv_main_item)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//关闭Glide的硬盘缓存机制
            ).into(iv_gerentg_item_icon);
            tv_gerentg_30.setText("" + mlist.get(position).getName());
            tv_gerentg_30_num.setText("" + mlist.get(position).getNum());
            tv_gerentg_30.setMovementMethod(ScrollingMovementMethod.getInstance());
            tv_gerentg_30.setHorizontallyScrolling(true); //不让超出屏幕的文本自动换行，使用滚动条
            tv_gerentg_30.setFocusable(true);
            return convertView;
        } else if (mtype == 4){
            convertView = View.inflate(mcontext, R.layout.pop_lv_gerentg_buy_item, null);
            LinearLayout ll_main_item = convertView.findViewById(R.id.ll_main_item);
            LinearLayout ll_pop_gerentg_1 = convertView.findViewById(R.id.ll_pop_gerentg_1);
            Button btn_pop_gerentg_1 = convertView.findViewById(R.id.btn_pop_gerentg_1);
            TextView tv_gerentg_1_ll1 = convertView.findViewById(R.id.tv_gerentg_1_ll1);
            TextView tv_gerentg_2_ll1 = convertView.findViewById(R.id.tv_gerentg_2_ll1);
            TextView tv_gerentg_3_ll1 = convertView.findViewById(R.id.tv_gerentg_3_ll1);
            TextView tv_gerentg_4_ll1 = convertView.findViewById(R.id.tv_gerentg_4_ll1);
            ViewGroup.LayoutParams layoutParams = ll_main_item.getLayoutParams();
            layoutParams.width = screenWidth / 5 * 2;
            ll_main_item.setLayoutParams(layoutParams);
            tv_gerentg_1_ll1.setText("" + detailList.get(position).getNum());
            tv_gerentg_3_ll1.setText("" + detailList.get(position).getPrice());
            if (position == select){
                btn_pop_gerentg_1.setVisibility(View.VISIBLE);
                ll_pop_gerentg_1.setBackgroundResource(R.drawable.pop_gerentg_item);
                tv_gerentg_1_ll1.setTextColor(Color.parseColor("#FF3B30"));
                tv_gerentg_2_ll1.setTextColor(Color.parseColor("#FF3B30"));
                tv_gerentg_3_ll1.setTextColor(Color.parseColor("#FF8178"));
                tv_gerentg_4_ll1.setTextColor(Color.parseColor("#FF8178"));
            } else {
                btn_pop_gerentg_1.setVisibility(View.GONE);
                ll_pop_gerentg_1.setBackgroundResource(R.drawable.login_exit);
                tv_gerentg_1_ll1.setTextColor(Color.parseColor("#000000"));
                tv_gerentg_2_ll1.setTextColor(Color.parseColor("#000000"));
                tv_gerentg_3_ll1.setTextColor(Color.parseColor("#B8BDC6"));
                tv_gerentg_4_ll1.setTextColor(Color.parseColor("#B8BDC6"));
            }
            return convertView;
        } else {
            convertView = View.inflate(mcontext, R.layout.lv_vip_item, null);
            LinearLayout ll_main_item = convertView.findViewById(R.id.ll_main_item);
            TextView tv_lv_vip_item_money_1 = convertView.findViewById(R.id.tv_lv_vip_item_money_1);
            TextView tv_vip_zx_month = convertView.findViewById(R.id.tv_vip_zx_month);
            TextView tv_vip_zx_month_name = convertView.findViewById(R.id.tv_vip_zx_month_name);
            TextView tv_lv_vip_item_money = convertView.findViewById(R.id.tv_lv_vip_item_money);
            RelativeLayout rl_vip_item_is = convertView.findViewById(R.id.rl_vip_item_is);
            ViewGroup.LayoutParams layoutParams = ll_main_item.getLayoutParams();
            layoutParams.width = screenWidth / 5 * 2;
            ll_main_item.setLayoutParams(layoutParams);
            // 中间加横线 ， 添加Paint.ANTI_ALIAS_FLAG是线会变得清晰去掉锯齿
            tv_vip_zx_month.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG );
            // 底部加横线 ， 添加Paint.ANTI_ALIAS_FLAG是线会变得清晰去掉锯齿
//            tv_vip_zx_month.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG );
            tv_vip_zx_month.setMovementMethod(ScrollingMovementMethod.getInstance());
            tv_vip_zx_month.setHorizontallyScrolling(true); //不让超出屏幕的文本自动换行，使用滚动条
            tv_vip_zx_month.setFocusable(true);
            tv_lv_vip_item_money.setText("" + mlist.get(position).getMoney() + "元/月");
            tv_lv_vip_item_money_1.setText("" + mlist.get(position).getMoney());
            tv_vip_zx_month.setText("原价：" + mlist.get(position).getReal_money() + "元");
            tv_vip_zx_month_name.setText("" + mlist.get(position).getName());
            if (mlist.get(position).getIs_limit() == 1){
                rl_vip_item_is.setVisibility(View.VISIBLE);
            } else {
                rl_vip_item_is.setVisibility(View.GONE);
            }
            return convertView;
        }
    }
}
